package com.qianfeng.missfood.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.util.List;

/**
 * Created by Administrator on 15-8-18.
 *
 *
 */
public class AddDotsUtils implements ViewPager.OnPageChangeListener {

    private Button dotBtn;
    private FrameLayout frameLayout;
    private LinearLayout linearLayout;
    private List list;
    private ViewPager viewPager;
    private Context context;

    private int focus;
    private int unfocus;

    public AddDotsUtils(Context context,FrameLayout frameLayout, ViewPager viewPager, List list, int focus, int unfocus) {
        this.context = context;
        this.frameLayout = frameLayout;
        this.viewPager = viewPager;
        this.list = list;
        this.focus = focus;
        this.unfocus = unfocus;
        viewPager.addOnPageChangeListener(this);
        addDots();
    }

    private void addDots() {

        linearLayout = new LinearLayout(context);
        FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        fl.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        fl.setMargins(0, 0, 0, 30);
        linearLayout.setLayoutParams(fl);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), unfocus);
        for (int i = 0; i < list.size(); i++) {

            Button bt = new Button(context);
            LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());
//            bt.setLayoutParams(new ViewGroup.LayoutParams(bitmap.getWidth(), bitmap.getHeight()));
            ll.setMargins(3, 0, 3, 0);
            bt.setLayoutParams(ll);
            bt.setBackgroundResource(unfocus);
            linearLayout.addView(bt);

        }

        frameLayout.addView(linearLayout);


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        if (dotBtn != null) {

            dotBtn.setBackgroundResource(unfocus);
        }

        Button currenButton = (Button) linearLayout.getChildAt(position);
        currenButton.setBackgroundResource(focus);
        dotBtn = currenButton;



    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
